/* eslint-disable @typescript-eslint/no-use-before-define */
import type { FormInstance } from "antd";
import { invokeFunc, toNamePath, shallowClone } from ".";
import type { DataType, NamePath } from "../types";

/**
 * 处理移动规则项逻辑
 */
export function moveRuleItem(
  form: FormInstance,
  from: {
    levelPath?: number[];
    index: number;
  },
  to: {
    levelPath?: number[];
    index: number;
  },
  childrenKey: string
) {
  const fromLevelPath = from.levelPath || [];
  const toLevelPath = to.levelPath || [];

  // notice: 判断是向上移动还是向下移动，涉及到先移动还是先插入
  const upword = invokeFunc(() => {
    const fromAllPath = [...fromLevelPath, from.index];
    const toAllPath = [...toLevelPath, to.index];

    let a = 0;
    let b = 0;
    while (fromAllPath.length > 0 && toAllPath.length > 0) {
      a = fromAllPath.shift()!;
      b = toAllPath.shift()!;
    }

    return b <= a;
  });

  const fromPath = toNamePath(fromLevelPath, childrenKey);
  const toPath = toNamePath(toLevelPath, childrenKey);
  const fromChildren = form.getFieldValue(fromPath) as unknown[];
  const cacheRemoveItem = shallowClone(fromChildren[from.index]);

  if (upword) {
    removeRuleItem(form, fromPath, from.index);
    addRuleItem(form, toPath, cacheRemoveItem, to.index);
  } else {
    addRuleItem(form, toPath, cacheRemoveItem, to.index);
    removeRuleItem(form, fromPath, from.index);
  }
}

/**
 * 处理新增规则项逻辑
 */
export function addRuleItem(
  form: FormInstance,
  path: NamePath,
  defaultValue?: unknown,
  insertIndex?: number
) {
  const currentList = form.getFieldValue(path) || [];
  const newList = [...currentList];
  const index = insertIndex === undefined ? currentList.length : insertIndex;
  newList.splice(index, 0, defaultValue || {});
  form.setFieldValue(path, newList);
}

/**
 * 处理新增规则组逻辑
 */
// eslint-disable-next-line max-params
export function addRuleGroup(
  form: FormInstance,
  path: NamePath,
  childrenKey: string,
  defaultValue?: DataType,
  insertIndex?: number
) {
  const currentList = form.getFieldValue(path) || [];
  const newList = [...currentList];
  const index = insertIndex === undefined ? currentList.length : insertIndex;
  newList.splice(index, 0, {
    [childrenKey]: [{}],
    ...defaultValue,
  });
  form.setFieldValue(path, newList);
}

/**
 * 处理rule-item删除逻辑
 */
export function removeRuleItem(
  form: FormInstance,
  path: NamePath,
  removeIndex: number,
  isRoot = false
) {
  const currentList = form.getFieldValue(path) || [];
  // notice: 根节点或最后一项不是最后一个时只删除当前项，否则就删除整个节点
  if (isRoot || currentList.length !== 1) {
    const newList = [...currentList];
    newList.splice(removeIndex, 1);
    form.setFieldValue(path, newList);
  } else {
    let parentPath = [...path];

    // eslint-disable-next-line no-constant-condition
    while (true) {
      const index = parentPath.at(-2) as number;
      parentPath = parentPath.slice(0, -2);
      const parent = form.getFieldValue(parentPath);
      const newParent = [...parent];
      newParent.splice(index, 1);
      const isEmpty = newParent.length === 0;
      if (!isEmpty || parentPath.length <= 1) {
        form.setFieldValue(parentPath, newParent);
        return;
      }
    }
  }
}
